package com.linzhaowei.headhuntingservice;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hyphenate.chat.EMClient;
import com.linzhaowei.headhuntingservice.bean.Interviewer;
import com.linzhaowei.headhuntingservice.utils.HttpUtils;
import com.linzhaowei.headhuntingservice.utils.Ip;

import java.io.IOException;

public class InterviewerUpdatePersonal extends AppCompatActivity implements View.OnClickListener {
    private TextView title;
    private EditText name;
    private EditText tel;
    private EditText company_name;
    private EditText position;
    private Button btn;

    private Interviewer interviewer;
    SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_improve_interviewer);
        sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
        initViews();
        setEvents();

        title.setText("修改个人信息");


        selectInterviewer();

    }

    private void initViews() {
        title = findViewById(R.id.i_i_title);
        name = findViewById(R.id.i_i_name);
        tel = findViewById(R.id.i_i_tel);
        company_name = findViewById(R.id.i_i_company);
        position = findViewById(R.id.i_i_position);
        btn = findViewById(R.id.i_i_btn);
    }

    private void setEvents() {
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.i_i_btn:
                improveInterviewer();

                break;
            default:
                break;

        }
    }

    private void improveInterviewer() {
        interviewer = new Interviewer();
        interviewer.setId(sharedPreferences.getInt("id", -1));
        interviewer.setName(name.getText().toString().trim());
        interviewer.setTel(tel.getText().toString().trim());
        interviewer.setCompany(company_name.getText().toString().trim());
        interviewer.setPosition(position.getText().toString().trim());
        String string = JSON.toJSONString(interviewer);

        new Thread() {
            @Override
            public void run() {
                HttpUtils httpUtils = new HttpUtils();

                try {
                    String result = httpUtils.post(Ip.ip + "/user/improve_interviewer", string);
                    if (result.equals("true")) {
                        if (!interviewer.getName().isEmpty()) {
                            EMClient.getInstance().pushManager().updatePushNickname(interviewer.getName());
                        }
                        runOnUiThread(() -> Toast.makeText(InterviewerUpdatePersonal.this, "修改成功", Toast.LENGTH_SHORT).show());
                        finish();
                    } else {
                        runOnUiThread(() -> Toast.makeText(InterviewerUpdatePersonal.this, "信息有误", Toast.LENGTH_SHORT).show());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }.start();
    }

    private void selectInterviewer() {

        new Thread() {
            @Override
            public void run() {
                HttpUtils httpUtils = new HttpUtils();

                try {

                    String result = httpUtils.get(Ip.ip + "/user/select_interviewer?id=" + String.valueOf(sharedPreferences.getInt("id", -1)));
                    JSONArray jsonArray = JSON.parseArray(result);
                    JSONObject jsonObject = jsonArray.getJSONObject(0);
                    interviewer = jsonObject.toJavaObject(Interviewer.class);
                    show();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private void show() {
        runOnUiThread(() -> {
            name.setText(interviewer.getName());
            tel.setText(interviewer.getTel());
            company_name.setText(interviewer.getCompany());
            position.setText(interviewer.getPosition());
        });
    }
}
